package cn.project.lemontree.controller;

import cn.project.lemontree.aop.SystemControllerLog;
import cn.project.lemontree.bean.*;
import cn.project.lemontree.service.PcTaskService;
import cn.project.lemontree.tools.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/pctask")
@Api(tags = "亲子任务管理模块")
@CrossOrigin  //跨域访问
public class PcTaskController {
    @Resource
    private PcTaskService pcTaskService;

    @ApiOperation("分页查询亲子任务信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name="page",value="页码",required=true,dataTypeClass = int.class,example = "1"),
            @ApiImplicitParam(name="size",value="每页行数",required=true,dataTypeClass = int.class,example = "10"),
            @ApiImplicitParam(name="theme",value="主题",required=false,dataTypeClass = String.class),
            @ApiImplicitParam(name="childName",value="儿童姓名",required=false,dataTypeClass = String.class)
    })
    @GetMapping("/listpage")
    public CommonResult<Map<String, Object>> listpage(int page, int size,
                                                      String childName, String theme){
        Map<String ,Object> map = new HashMap<>();
        map.put("data",pcTaskService.listpage(page,size,childName,theme));
        map.put("count",pcTaskService.count(childName,theme));
        map.put("page",page);
        return CommonResult.success(map);
    }

    @ApiOperation(value = "家长领取亲子任务")
    @PostMapping("/save")
    public  CommonResult savePCTask(@RequestBody Parent_child_task pCTask, @RequestParam Integer parentId, @RequestParam Integer childId){
        Parent parent=new Parent();
        Child child=new Child();
        parent.setParentId(parentId);
        child.setChildId(childId);
        pCTask.setParent(parent);
        pCTask.setChild(child);
        return CommonResult.success(pcTaskService.savePCTask(pCTask));
    }

    @SystemControllerLog(description = "变更亲自任务")
    @ApiOperation(value = "家长变更亲子任务")
    @PostMapping("/update")
    public  CommonResult updatePCTask(@RequestBody Parent_child_task pCTask, @RequestParam Integer parentId, @RequestParam Integer childId){
        Parent parent=new Parent();
        Child child=new Child();
        parent.setParentId(parentId);
        child.setChildId(childId);
        pCTask.setParent(parent);
        pCTask.setChild(child);
        return CommonResult.success(pcTaskService.updatePCTask(pCTask));
    }
    @SystemControllerLog(description = "删除亲子任务")
    @ApiOperation("删除亲子任务")
    @GetMapping("/remove")
    public CommonResult remove(int p_c_task_id){
        return CommonResult.success(pcTaskService.removeById(p_c_task_id));
    }

    @SystemControllerLog(description = "通过儿童查找亲子任务")
    @ApiOperation("通过儿童查找未完成亲子任务")
    @GetMapping("/findbychild")
    public CommonResult findByChild(@RequestParam Integer childId){
        return CommonResult.success(pcTaskService.findAllByChild(childId));
    }
}
